import vue from 'vue'
import vuex from 'vuex'
import module from '@/const'
import axios from 'axios'
import {post} from '@/http'
import router from '@/router'
vue.use(vuex);
var store = new vuex.Store({
  //所有的模块放入modules
  modules: {},
  state: {
    id: sessionStorage.id,
  },
  mutations: {
    setStateUser(state, user = null) {
      sessionStorage.id=user.id;
      state.id = user.id;
    },
  },
  actions: {
    loginUser(context, user) {
      return new Promise((resolve, reject) => {
        post('/ajaxLogin', user).then(data => {
          context.commit("setStateUser", data.datas);
          let children=[];
          /*for (let i=0;i<data.datas.modules.length;i++){
            for (let j=0;j<data.datas.modules[i].moduleVos.length;j++){
              let module=data.datas.modules[i].moduleVos[j];
              children.push({path:module.url,component:resolve=>require([module.component],resolve)});
            }
          }*/
          let module={};
          module.url='/user/pages';
          children.push({path:module.url,component:resolve=>require(['@/page/table.vue'],resolve)});
          router.addRoutes([{path:'/my',component:resolve => require(['@/page/my.vue'], resolve),children:children}]);
          resolve();
        }).catch(err => {
          reject(err)
        });
      });
    }
  }
});
export default store;
